package cn.tiasen.helloworld;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;

import java.util.Calendar;


public class DialogActivity extends AppCompatActivity {

    private Button btn_ad;
    private Button btn_ad2;
    private Button btn_ad3;
    private Button btn_ad4;
    private Button btn_ad5;
    private Button btn_ad6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);

        btn_ad = findViewById(R.id.btn_ad);
        btn_ad2 = findViewById(R.id.btn_ad2);
        btn_ad3 = findViewById(R.id.btn_ad3);
        btn_ad4 = findViewById(R.id.btn_ad4);
        btn_ad5 = findViewById(R.id.btn_ad5);
        btn_ad6 = findViewById(R.id.btn_ad6);

        btn_ad.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new AlertDialog.Builder(DialogActivity.this)
                        .setTitle("删除")
                        .setMessage("确定删除？")
                        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"删除",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"取消",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        btn_ad2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String[] colors = {"红","黄","蓝","绿"};
                new AlertDialog.Builder(DialogActivity.this)
                        .setTitle("选择背景")
                        .setSingleChoiceItems(colors, 2, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),colors[which],Toast.LENGTH_SHORT).show();
                                dialog.dismiss();
                            }
                        })
                        .show();
            }
        });
        btn_ad3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LayoutInflater  inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                View view = inflater.inflate(R.layout.dialog_custom, null);
                new AlertDialog.Builder(DialogActivity.this)
                        .setView(view)
                        .setPositiveButton("登录", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"登录",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(getApplicationContext(),"取消",Toast.LENGTH_SHORT).show();
                            }
                        })
                        .show();
            }
        });

        btn_ad4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog = ProgressDialog.show(DialogActivity.this, "数据加载", "数据加载中...");
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                            dialog.dismiss();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();

            }
        });
        btn_ad5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final ProgressDialog dialog = new ProgressDialog(DialogActivity.this);
                dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                dialog.show();
                new Thread(){
                    @Override
                    public void run() {
                        try {
                            for(int i = 0; i <= 100; i++){
                                dialog.setProgress(i);
                                Thread.sleep(100);
                            }

                            dialog.dismiss();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });

        btn_ad6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);
                System.out.println(year + "-" + month + "-" + day);
                new DatePickerDialog(DialogActivity.this, new DatePickerDialog.OnDateSetListener() {
                    @Override
                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                        System.out.println(year + "--" + month + "--" + dayOfMonth);
                    }
                },year,month,day);
            }
        });
    }
}
